package com.medi.business.service.impl;

import com.medi.business.domain.UserRelation;
import com.medi.business.mapper.UserRelationMapper;
import com.medi.business.service.IUserRelationService;
import com.medi.common.utils.DateUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户多对多关联关系Service业务层处理
 *
 * @author medi
 * @date 2024-09-13
 */
@Service
public class UserRelationServiceImpl implements IUserRelationService {
    @Autowired
    private UserRelationMapper userRelationMapper;

    /**
     * 查询用户多对多关联关系
     *
     * @param id 用户多对多关联关系主键
     * @return 用户多对多关联关系
     */
    @Override
    public UserRelation selectUserRelationById(String id) {
        return userRelationMapper.selectUserRelationById(id);
    }

    /**
     * 查询用户多对多关联关系列表
     *
     * @param userRelation 用户多对多关联关系
     * @return 用户多对多关联关系
     */
    @Override
    public List<UserRelation> selectUserRelationList(UserRelation userRelation) {
        return userRelationMapper.selectUserRelationList(userRelation);
    }

    @SneakyThrows
    public String GetObjectIds(String userId, Integer category) {

        String res = "";
        UserRelation userRelation = new UserRelation();
        userRelation.setUserId(userId);
        userRelation.setRelationType(category);
        List<UserRelation> list = selectUserRelationList(userRelation);
        if (list == null || list.size() == 0) {
            return res;
        }
        for (UserRelation userRelationEntity : list) {
            if (res != "") {
                res += ",";
            }
            res += userRelationEntity.getObjectId();
        }
        return res;
    }

    /**
     * 新增用户多对多关联关系
     *
     * @param userRelation 用户多对多关联关系
     * @return 结果
     */
    @Override
    public int insertUserRelation(UserRelation userRelation) {
        userRelation.setCreateTime(DateUtils.getNowDate());
        return userRelationMapper.insertUserRelation(userRelation);
    }

    /**
     * 修改用户多对多关联关系
     *
     * @param userRelation 用户多对多关联关系
     * @return 结果
     */
    @Override
    public int updateUserRelation(UserRelation userRelation) {
        userRelation.setUpdateTime(DateUtils.getNowDate());
        return userRelationMapper.updateUserRelation(userRelation);
    }

    /**
     * 批量删除用户多对多关联关系
     *
     * @param ids 需要删除的用户多对多关联关系主键
     * @return 结果
     */
    @Override
    public int deleteUserRelationByIds(String[] ids) {
        return userRelationMapper.deleteUserRelationByIds(ids);
    }

    /**
     * 删除用户多对多关联关系信息
     *
     * @param id 用户多对多关联关系主键
     * @return 结果
     */
    @Override
    public int deleteUserRelationById(String id) {
        return userRelationMapper.deleteUserRelationById(id);
    }
}
